package com.app.newiptv.presenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.bean.Content;
import com.app.newiptv.bean.VodListBean;
import com.app.newiptv.bean.details.PlayDetailsBean;
import com.app.newiptv.callback.ContentCallback;
import com.app.newiptv.callback.IRequestCallback;
import com.app.newiptv.service.AHservice;
import com.app.newiptv.utils.APPLog;

import java.util.List;


/**
 * Created by wendal on 2017/9/15.
 */

public class VodPresenter {
    private boolean isDisplayed;
    private Callback callback;
    private List<Content> listContent;
    private List<Content> vodContent;
    private VodListBean vodListBean;
    private List<PlayDetailsBean> detailsBeanList;
    private AHservice service;
    private Context mContext;

    public VodPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(VodPresenter presenter, int count);

        void onVodLoadCompleted(VodPresenter presenter, int count);

        void onDetailCompleted(VodPresenter presenter);
    }

    public interface ViewHolder {
        void setName(String name);
    }

    public interface ItemViewHolder {
        void setImage(String imageUrl);

        void setItemName(String name);
    }

    public interface IDetailDataViewHolder {
        void setNameAndCount(String name, String count);
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    //加载数据
    public void loadItem(String url, Callback callback) {
        this.callback = callback;
        service = new AHservice();
        service.loadVodListData(url, new ItemLoadCallback());
    }

    public void loadVodItem(int postion) {
        //service.loadVodItemData("", new VodItemLoadCallBack());
        if (vodListBean != null && vodListBean.getChildren() != null && vodListBean.getChildren().size() > 0 && postion < vodListBean.getChildren().size()) {
            detailsBeanList = vodListBean.getChildren().get(postion).getVideos();
            if (detailsBeanList != null) {
                callback.onVodLoadCompleted(VodPresenter.this, detailsBeanList.size());
            }
        }
    }

    public void onBindDetailData(IDetailDataViewHolder viewHolder) {


    }

    public void setPosition(int position) {
        if (detailsBeanList != null && position < detailsBeanList.size()) {
            PlayDetailsBean latelyBean = detailsBeanList.get(position);
            Intent intent = new Intent();
            if(getVodType(position)){
                intent.setClass(mContext, FilmDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("id",latelyBean.getId());
                intent.putExtra("bundle",bundle);
                mContext.startActivity(intent);
            }else if(getTelType(position)){
                intent.setClass(mContext, SitcomActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("id",latelyBean.getId());
                intent.putExtra("bundle",bundle);
                mContext.startActivity(intent);
            }
        }
    }

    public void onBindMovieView(ViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        if (vodListBean != null && vodListBean.getChildren() != null && vodListBean.getChildren().size() > 0 && position < vodListBean.getChildren().size()) {
            holder.setName(vodListBean.getChildren().get(position).getName());
        }
    }

    public void onBindItem(ItemViewHolder viewHolder, int position) {
        if (!isDisplayed) {
            return;
        }
        if (detailsBeanList != null && detailsBeanList.size() > 0 && position < detailsBeanList.size()) {
            viewHolder.setImage(detailsBeanList.get(position).getImage());
            viewHolder.setItemName(detailsBeanList.get(position).getName());
        }
    }

    class ItemLoadCallback implements IRequestCallback {
        @Override
        public void onFailed(String arg0, String arg1) {
        }

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, Object dto) {
            if (callback == null || !isDisplayed) {
                APPLog.printInfo("onLoadComplete:callback :");
                return;
            }

            if (dto != null && dto instanceof VodListBean) {
                vodListBean = (VodListBean) dto;
                callback.onMovieCompleted(VodPresenter.this, vodListBean.getChildren().size());
            }

        }

        @Override
        public void onLoading(String arg0) {
        }
    }

    class VodItemLoadCallBack implements ContentCallback {

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, List<Content> contents) {
            vodContent = contents;
            callback.onVodLoadCompleted(VodPresenter.this, count);
        }

        @Override
        public void onLoading(String contentID) {

        }

        @Override
        public void onFailed(String code, String msg) {

        }
    }
    public boolean getVodType(int position) {
        PlayDetailsBean playDetailsBean = detailsBeanList.get(position);
        if(playDetailsBean.getTotal_episode()==1){
            return true;
        }
        return false;
    }

    public boolean getTelType(int position) {
        PlayDetailsBean playDetailsBean = detailsBeanList.get(position);
        if(playDetailsBean.getTotal_episode()>1){
            return true;
        }
        return false;
    }
}
